// 新建窗口
import { ipcMain } from 'electron'
import { WindowCreator } from '../hooks/createdWindow'
let win: WindowCreator | null = null
const createWindow = (name: string, login: boolean) => {
  //判断是否创建过窗口,如果创建过置顶
  if (win && win.window && !win.window.isDestroyed()) {
    win.show()
    return
  }

  // 创建新窗口
  win = new WindowCreator({
    title: name,
    width: 680,
    height: 567,
    frame: false, // 无边框
    transparent: true, // 透明
    skipTaskbar: true, // 不在任务栏显示
    entry: 'index.html#/loginPage?login=' + login
  })
  win.show()
}
// 注册登录窗口页面
function loginPageWindow() {
  ipcMain.handle('loginPage-window', async (_, name: string, login: boolean) => {
    createWindow(name || '登录', login)
  })
  ipcMain.handle('loginPage-window-close', async (_) => {
    if (win && win.window && !win.window.isDestroyed()) {
      win?.window?.close()
      win = null
    }
  })
}
export default {
  loginPageWindow
}
